//
//                 rtmeus.h: written by T.Yoshikai 2010/05/24
//
#ifndef RTMEUS_H_
#define RTMEUS_H_

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <rtm/Manager.h>
#include <rtm/CorbaNaming.h>
#include <rtm/NVUtil.h>
#include <services/CommandService/CommandService.hh>
#include <services/StateService/StateService.hh>
#include <services/StateService/StateManager.h>
#include <services/StateService/State.h>

#define MANBASEPORT 9876
#define MAXCOMPS 64

class RTMEusComp {
public:
    RTMEusComp(const char* cpath, const char* cname, bool cflag);
    RTMEusComp(const char* cpath, const char* cname, bool cflag, RTMEUS::StateService_var svc);
    ~RTMEusComp();
    std::string Activate();
    std::string Deactivate();
    std::string SetConfiguration(const char* configvar, const char* config);
    std::string Destroy();
    std::string GetComponentPath();
    std::string GetComponentName();
    std::string ShowComponentInfo(int showlevel);
    std::string InputData(const char* port, const char* datatype, const char* data);
    std::string ReceiveData(const char* port, const char* datatype);
    std::string GetStates(std::vector<std::string> strnames);
    std::string SetStates(coil::Properties prop);
private:
    bool createflag;
    char m_componentpath[128];
    char m_componentname[128];
    RTMEUS::StateService_var stsvc;
};

class RTMEusManager {
public:
    RTMEusManager(char* host, int port, int manager_no);
    ~RTMEusManager();
    std::string CreateManager(char* host, int port, int manager_no);
    std::string LoadModule(const char* sopath, const char* initfuncname);
    std::string UnLoadModule(const char* sopath);
    std::string CreateModule(const char* classname, const char* instancename, double exec_rate, const char* exec_type);
    CORBA::Object_ptr UseServiceFromManager(const char* rtcpath, const char* servicetype, const char* serviceinterface);
    std::string SendCommandFromManager(const char* rtcname, const char* command, const char* argv);
    void PluginModeAllActivateFromManager(const char* leader_rtcname, std::vector<std::string> attatched_rtcnames, int interval);
    void PluginModeAllDeactivateFromManager(const char* leader_rtcname, std::vector<std::string> attatched_rtcnames);
    void SetPluginModeFromManger(const char* leader_rtcname, std::vector<std::string> attatched_rtcnames);
    
private:
    RTC::CorbaNaming *m_naming;
    RTC::Manager* m_manager;
    char m_managerpath[128];
    char m_host[64];
};

class RTMEus{
public:
    RTMEus(char* host, int port, int manager_no);
    ~RTMEus();
    std::string LoadModule(const char* sopath, const char* initfuncname);
    int CreateComponent(const char* classname, const char* instancename, double exec_rate, const char* exec_type, bool sflag);
    int ResolveComponent(const char* searchpath, const char* instancename, bool sflag);
    int FindComponent(const char* searchpath, const char* instancename);
    RTMEusComp* GetComponent(int component_no);
    std::string SendCommandService(const char* compname, const char* command, const char* argv);
    void SetPluginMode(const char* leader_rtcname, std::vector<std::string> attatched_rtcnames);
    void PluginModeAllActivate(const char* leader_rtcname, std::vector<std::string> attatched_rtcnames, int interval);
    void PluginModeAllDeactivate(const char* leader_rtcname, std::vector<std::string> attatched_rtcnames);
    static std::string CommandExec(const char* command, const char* args);        
    static std::string ConnectComponents(const char* outRtcPath, const char* outPort, const char* inRtcPath, const char* inPort, int subscriptionType);
    static std::string DisconnectComponents(const char* outRtcPath, const char* outPort, const char* inRtcPath, const char* inPort);
private:
    RTMEusManager *rtmeus_manager;
    std::vector<RTMEusComp*> comps;
};

#endif // RTMEUS_H_
